//= require <dom/sensors/Sensor>

namespace("w3c.sensors.ProximitySensor", {
	'@inherits' : w3c.sensors.Sensor,
	
	initialize : function(control, radius){
		this.radius = radius;
		this.control = control;
		this.element = this.control.element;
	},
	
	addEventListener : function(type, callback, capture) {
		var self=this;
		if(type.toLowerCase() == "inproximity") {
			document.addEventListener("mousemove", function(e){
				if(self.inRadialDistance(e)) {
					var center= {
						x : xPageX(self.element) + (xWidth(self.element)/2),
						y : xPageY(self.element) + (xHeight(self.element)/2)
					};
					var distance = parseInt(Math.sqrt(Math.pow(e.pageX-center.x,2) + Math.pow(e.pageY-center.y,2)));
					e.distanceFromCenter = distance;
					e.distanceRatio = Math.round((e.clientX/center.x)*10)/10;
					
					callback(e);
				};
			},false);
		}
		else {
			this.parent(type, callback, capture);
		}		
	},
	
	inRadialDistance : function(e){
		return xHasPoint(this.element,e.clientX,e.clientY,this.radius);
	}
});